filePath=$1 #原文件完整路径
serialType=$2 #签名车类型
fileDir=$(dirname "$filePath") # 原文件目录
fileName=$(basename "$filePath") # 原文件的名称
# fileName=$(basename -s ".apk"  "$filePath") # 原文件的名称（不带后缀）
currentTime=$(date '+%Y%m%d%H%M%S')
tempDir=${fileDir}/$currentTime
newApkFilePath=${fileDir}/sign-$fileName

mkdir -p $tempDir

if [[ $serialType == "wutong" ]]
then
    #  echo 梧桐系
     serial="0xddb66eefd98476f3" 
elif [[ $serialType == "feiyu" ]]
then
    #  echo 飞鱼系
     serial="0xd42599c0446bdafc"
elif [[ $serialType == "g318" ]]
then
    #  echo G318
     serial="0xb3998086d056cffa"
elif [[ $serialType == "a07" ]]
then
    #  echo A07
     serial="7b06e022411a04e1e0746ba461af8017ea34fa08"
else
     echo 没有对应该车型
fi

echo ------------------------------生成私钥：private.key---------------------;
openssl genrsa -out $tempDir/private.key 2048

echo ------------------------------生成：csr.csr和certificate.crt---------------------;
openssl req -new -key $tempDir/private.key -out $tempDir/csr.csr -subj "/emailAddress=auto_release@auto-pai.com/CN=SCM/OU=Software/O=WTCL/L=HaiDian/ST=Beijing/C=CN"
openssl x509 -req -in $tempDir/csr.csr -signkey $tempDir/private.key -out $tempDir/certificate.crt -days 18250 -set_serial $serial

echo ------------------------------生成：cert.p12---------------------;
openssl pkcs12 -export -in $tempDir/certificate.crt -inkey $tempDir/private.key -out $tempDir/cert.p12 -name cert -passout pass:123456789

echo ------------------------------生成：cert.jks---------------------;
keytool -importkeystore -srckeystore $tempDir/cert.p12 -srcstorepass 123456789 -srcstoretype PKCS12 -destkeystore $tempDir/cert.jks -deststoretype JKS -deststorepass 123456789 -noprompt

echo ------------------------------开始签名---------------------;
apksigner sign --ks $tempDir/cert.jks --ks-key-alias "cert" --ks-pass pass:123456789 --v1-signing-enabled true --v2-signing-enabled true --v3-signing-enabled false --out $newApkFilePath $filePath


echo ------------------------------删除临时文件---------------------;
rm -rf $tempDir
rm -rf $newApkFilePath'.idsig'


result="file>>>$newApkFilePath<<<file"
echo ------------------------------输出结果---------------------;



echo --123--

echo $result


